﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Leadyo.WebService
{
    public class BaseBLL<T> where T : class, new()
    {
        /// <summary>
        /// 基础数据访问层接口对象
        /// </summary>
     
        private string dalName = "";

        /// <summary>
        /// BLL业务类的全名（子类必须实现），可使用this.GetType().FullName
        /// </summary>
        protected string bllFullName;

        /// <summary>
        /// 数据访问层程序集的清单文件的文件名，不包括其扩展名，可使用Assembly.GetExecutingAssembly().GetName().Name
        /// </summary>
        protected string dalAssemblyName;

        /// <summary>
        /// BLL命名空间的前缀（BLL.)
        /// </summary>
        protected string bllPrefix = "BLL.";

        /// <summary>
        /// 基础数据访问层接口对象
        /// </summary>
        protected IBaseDAL<T> baseDal = null;

        protected void Init(string bllFullName, string dalAssemblyName=null)
        {
            //if (string.IsNullOrEmpty(bllFullName))
            //    throw new ArgumentNullException("子类未设置bllFullName业务类全名！");

            //if (string.IsNullOrEmpty(dalAssemblyName))
            //{
            //    dalAssemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            //}
            this.bllFullName = bllFullName;
            this.dalAssemblyName = dalAssemblyName;
            this.bllPrefix = bllPrefix;
            string dbType = "sqlserver";
            string DALPrefix = "";
            if (dbType == "sqlserver")
            {
                DALPrefix = "DalServer.";
            }
            //赋值，准备构建对象
            this.dalName = bllFullName.Replace(bllPrefix, DALPrefix);//替换中级的BLL.为DAL.，就是DAL类的全名
            this.dalAssemblyName = "Leadyo.DalServer";
            baseDal = Reflect<IBaseDAL<T>>.Create(this.dalName, this.dalAssemblyName);//构造对应的DAL数据访问层的对象类
            //Insert(obj);
        }
        /// <summary>
        /// 插入指定对象到数据库中
        /// </summary>
        /// <param name="obj">指定的对象</param>
        /// <param name="trans">事务对象</param>
        /// <returns>执行操作是否成功。</returns>
        public virtual bool Insert(T obj)
        {
            if (baseDal.Insert(obj) > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 插定要删除的数据
        /// </summary>
        /// <param name="key">指定主鍵</param>
        /// <returns>执行是操作是否成功</returns>
        public virtual bool Delete (string key)
        {
           return  baseDal.Delete( key);
        }
        /// <summary>
        /// 指定需要刷新的数据
        /// </summary>
        /// <param name="obj">指定要刷新的对象</param>
        /// <returns>执行刷新操作是否成功</returns>
        public virtual bool Update(T obj)
        {
            return baseDal.Update(obj);
        }
        /// <summary>
        /// 指定对象要返最后一条数据
        /// </summary>
        /// <returns>返最后一条数据</returns>
        public virtual List<T> GetLastEntriyList()
        {
            return baseDal.GetLastEntriyList();
        }
        /// <summary>
        /// 返回一个对像值
        /// </summary>
        /// <returns></returns>
        public virtual T GetModel(string sql)
        {
            return baseDal.GetModel( sql);
        }
        /// <summary>
        /// 检查数据是否存在
        /// </summary>
        /// <param name="TableName">表名</param>
        /// <param name="FieldName">字段</param>
        /// <param name="FieldValue">字段值</param>
        /// <returns>返回操作是否执行成功</returns>
        public virtual bool CheckNoExists(string TableName, string FieldName, string FieldValue)
        {
            return baseDal.CheckNoExists(TableName, FieldName, FieldValue);
        }
        /// <summary>
        /// 获取对象最新一条数据
        /// </summary>
        /// <returns>返回作操是否执行成功</returns>
        public virtual  List<T> GetFirsttEntriyList()
        {
            return baseDal.GetFirsttEntriyList();
        }
        /// <summary>
        /// SQL操作 返回一个表
        /// </summary>
        /// <param name="sql">构建SQL</param>
        /// <returns>返回一个表数据</returns>
        public virtual DataTable GetDataTableBySql(string sql)
        {
            return baseDal.GetDataTableBySql(sql);
        }
        /// <summary>
        /// SQL操作 返回一个表集合
        /// </summary>
        /// <param name="sql">构建SQL</param>
        /// <returns>返回一个表集合</returns>
        public virtual DataSet GetDataSetBySql(string sql)
        {
            return baseDal.GetDataSetBySql(sql);
        }
        /// <summary>
        /// 执行操作的行数
        /// </summary>
        /// <param name="sql">构建SQL操作的数据</param>
        /// <returns>返回操作执行成功的行数</returns>
        public virtual int ExecuteNonQuery(string sql)
        {
            return ExecuteNonQuery(sql);
        }
        ///// <summary>
        ///// 插入数据
        ///// </summary>
        ///// <param name="obj">对象</param>
        ///// <returns>返回操作据功的行数</returns>
        //public int Insert2(object obj)
        //{
        //   return baseDal.Insert2( obj);
        //}

    }
}
